﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RJ.Objects.PokerCards;
using Newtonsoft.Json;
using System.Drawing;
using System.IO;
using RJ.Objects.Workers;

namespace RJ.Euchre.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            var program = new Program();

            program.DealCards();
            program.WriteHands();
            program.WriteTopCard();
            program.WriteJsonDictionaryForCardFaces();
            program.ReadAndWriteToConsole();

            Console.ReadKey();
        }

        private EuchreDeck deck;
        private List<Hand> hands;
        private JsonFileReaderWriter<Dictionary<string, Point>> jsonReaderWriter;

        public Program()
        {
            deck = new EuchreDeck();
            hands = new List<Hand>()
            {
                new Hand(5),
                new Hand(5),
                new Hand(5),
                new Hand(5)
            };

            jsonReaderWriter = new JsonFileReaderWriter<Dictionary<string, Point>>(@"deckPoints.json");
        }

        public void DealCards()
        {
            deck.YatesShuffle();
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < hands.Count; y++)
                {
                    var hand = hands[y];

                    hand.ReceiveCards(deck.DealNextCards());
                }
            }
        }

        public void WriteHands()
        {
            for (int y = 0; y < hands.Count; y++)
            {
                var hand = hands[y];
                Console.WriteLine("Hand {0} ----------------------------\r\n", y + 1);
                foreach (var card in hand.Cards)
                {
                    Console.WriteLine("\t{0} of {1}", card.Face, card.Suit);
                }
                Console.WriteLine("");
            }
        }

        public void WriteTopCard()
        {
            Console.WriteLine("Possible Trump Suit {0}. Card: {1}", deck.TopCardOfLastFour.Suit, deck.TopCardOfLastFour.Face);
        }

        public void ReadAndWriteToConsole()
        {
            Dictionary<string, Point> dictOfPoints = jsonReaderWriter.Read();

            if (dictOfPoints != null && dictOfPoints.Count > 0)
            {
                foreach (var kvp in dictOfPoints)
                {
                    Console.WriteLine("Card: {0}, {{x:{1}, y:{2}}}", kvp.Key, kvp.Value.X, kvp.Value.Y);
                }
            }
        }

        public void WriteJsonDictionaryForCardFaces()
        {
            Deck fullDeck = new Deck(true);
            int height = 252;
            int width = 180;

            Dictionary<string, Point> playinCardsByName = fullDeck.Cards.ToDictionary(
                k => k.Name,
                (el) =>
                {
                    int faceValue = (int)el.Face;

                    if (el.Face == CardFace.AceHigh)
                        faceValue = (int)CardFace.AceLow;

                    int posY = Math.Abs((((int)el.Suit) - 4) * height);
                    int posX = (faceValue - 1) * width;

                    Point topPoint = new Point(posX, posY);

                    return topPoint;
                }
            );

            jsonReaderWriter.Write(playinCardsByName);
        }
    }
}
